﻿using AiXiu.Common;
using AiXiu.DAL;
using AiXiu.IBLL;
using AiXiu.IDAL;
using AiXiu.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AiXiu.BLL
{
    public class UserBLL : IUserBLL
    {
        /// <summary>
        /// 添加用户信息
        /// </summary>
        /// <param name="logins"></param>
        /// <returns></returns>
        public int AddUser(TBLogins logins)
        {
            IUserDAL userDAL = new UserDAL();
            int n=userDAL.AddUser(logins);
            return n;
        }

        /// <summary>
        /// 用户注册
        /// </summary>
        /// <param name="logins"></param>
        /// <returns></returns>
        public OperResult Register(TBLogins logins)
        {
            // 1) 用户名和手机号非空验证
            if (string.IsNullOrEmpty(logins.UserName))
            {
                return OperResult.Failed("用户名不能为空！");
            }
            if (string.IsNullOrEmpty(logins.MobileNumber))
            {
                return OperResult.Failed("手机号不能为空！");
            }
            if (string.IsNullOrEmpty(logins.Password))
            {
                return OperResult.Failed("密码不能为空！");
            }

            // 2) 判断用户名是否已注册（UserDAL中调用方法）
            IUserDAL userDAL = new UserDAL();
            bool flag = userDAL.ExistUserName(logins.UserName);
            if (flag)
            {
                return OperResult.Failed("该用户名已存在，请重新输入！");
            }


            //3) 判断手机号是否已注册（UserDAL中调用方法）
            bool flag1 = userDAL.ExistMobileNumber(logins.MobileNumber);
            if (flag1)
            {
                return OperResult.Failed("该手机号已存在，请重新输入！");
            }

            //4) 加密用户密码
            SHAEncryption sHAEncryption = new SHAEncryption();
            logins.Password= sHAEncryption.SHA1Encrypt(logins.Password);

            //  5)注册（UserDAL中调用方法）—— try catch
            try
            {
                userDAL.AddUser(logins);
            }
            catch (Exception ex)
            {

                return OperResult.Failed(ex.ToString());
            }
            return OperResult.Succeed();
        }

        /// <summary>
        /// 通过用户名密码登录
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public OperResult<TBUsers> LoginByUserName(string userName, string password)
        {
            //参数非空验证
            if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password))
            {
                return OperResult<TBUsers>.Failed("用户名和密码不能为空！");
            }

            //通过用户名查找登录信息，如果存在则继续
            IUserDAL userDAL = new UserDAL();
            TBLogins tBLogins= userDAL.FindByUserName(userName);
            if (tBLogins == null)
            {
                return OperResult<TBUsers>.Failed("该用户不存在，请重新输入用户名！");
            }
            //用户存在，验证密码
            SHAEncryption sHAEncryption = new SHAEncryption();
            if (tBLogins.Password!= sHAEncryption.SHA1Encrypt(password))
            {
                return OperResult<TBUsers>.Failed("密码输入错误！");
            }

            //获取用户的详细信息TBUsers
            TBUsers tBUsers = userDAL.FindByUserId(tBLogins.Id);
            return OperResult<TBUsers>.Succeed(tBUsers);
        }

        public OperResult<TBUsers> LoginByMobile(string mobile, string password)
        {
            //判断用户名和密码是否为空
            if (string.IsNullOrEmpty(mobile) || string.IsNullOrEmpty(password))
            {
                return OperResult<TBUsers>.Failed("手机号或密码不能为空！");
            }

            //通过手机号和密码查找用户
            IUserDAL userService = new UserDAL();
            TBLogins loginEntity = userService.FindByMobile(mobile);
            if (loginEntity == null)
            {
                return OperResult<TBUsers>.Failed("该用户不存在");
            }

            //验证用户的密码是否需正确
            SHAEncryption sHAEncryption = new SHAEncryption();
            //currentPwd 当前页面输入的密码进行加密
            string currentPwd = sHAEncryption.SHA1Encrypt(password);
            if (!currentPwd.Equals(loginEntity.Password))
            {
                return OperResult<TBUsers>.Failed("密码错误！");
            }

            //查找用户详细信息
            TBUsers user = userService.FindByUserId(loginEntity.Id);
            return OperResult<TBUsers>.Succeed(user);
        }

        /// <summary>
        /// 更新用户信息(不包含头像)
        /// </summary>
        /// <param name="user">用户信息</param>
        /// <returns></returns>
        public OperResult<TBUsers> EditUser(TBUsers user)
        {
            IUserDAL userService = new UserDAL();
            try
            {
                TBUsers profileEntity = userService.EditUser(user);
                return OperResult<TBUsers>.Succeed(profileEntity);
            }
            catch (Exception ex)
            {
                return OperResult<TBUsers>.Wrong(ex);
            }
        }

        /// <summary>
        /// 更新用户头像
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public OperResult<TBUsers> EditAvatar(TBUsers user)
        {
            IUserDAL userService = new UserDAL();
            try
            {
                TBUsers profileEntity = userService.EditAvatar(user);
                return OperResult<TBUsers>.Succeed(profileEntity);
            }
            catch (Exception ex)
            {
                return OperResult<TBUsers>.Wrong(ex);
            }
        }
    }
}
